📑   Links

In [ ]:

In [ ]:

📑   Library

In [1]:
R.version.string
'R version 3.3.3 (2017-03-06)'
In [1]:
library(repr)
In [6]:
library(ggplot2)
library(DT)
library(formattable)
library(plotly)
In [7]:
library(osmar)
library(maptools)
library(ggmap)
In [14]:
library(igraph)
library(leaflet)
In [40]:
library(networkD3)
library(RColorBrewer)
In [45]:
library(highcharter)
library(visNetwork)
library(d3heatmap)
In [4]:
ip <- as.data.frame(installed.packages()[,c(1,3:4)])
rownames(ip) <- NULL
ip <- ip[is.na(ip$Priority),1:2,drop=FALSE]
In [5]:
datatable(ip)
In [ ]:

In [ ]:

📑   Data

In [8]:
gc01 <- geocode("GUM Moscow", output = "more")
datatable(t(data.frame(gc01)))
Source : https://maps.googleapis.com/maps/api/geocode/json?address=GUM%20Moscow
In [9]:
formattable(data.frame(mapdist("GUM Moscow", "Bolshoy Theater")))
Source : https://maps.googleapis.com/maps/api/distancematrix/json?origins=GUM%20Moscow&destinations=Bolshoy%20Theater&mode=driving&language=en-EN
fromtomkmmilessecondsminuteshours
GUM Moscow Bolshoy Theater6093 6.093 3.78619 1135 18.91667 0.3152778
In [29]:
src <- osmsource_api()
bigbox <- center_bbox(38.6533, 55.7758, 3000, 3000)
bpavposad <- get_osm(bigbox, source = src)
str(bpavposad)
List of 3
 $ nodes    :List of 2
  ..$ attrs:'data.frame':	27411 obs. of  9 variables:
  .. ..$ id       : num [1:27411] 7.65e+07 2.54e+09 2.54e+09 2.54e+09 2.54e+09 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:27411], format: "2013-03-22 17:51:04" "2013-11-21 21:54:26" ...
  .. ..$ version  : num [1:27411] 3 1 1 1 1 1 1 1 1 1 ...
  .. ..$ changeset: num [1:27411] 15457006 19045860 19045860 19045860 19046068 ...
  .. ..$ user     : Factor w/ 67 levels "347929","Владимир К",..: 42 29 29 29 29 29 29 29 29 29 ...
  .. ..$ uid      : Factor w/ 67 levels "109327","1101663",..: 43 14 14 14 14 14 14 14 14 14 ...
  .. ..$ lat      : num [1:27411] 55.8 55.8 55.8 55.8 55.8 ...
  .. ..$ lon      : num [1:27411] 38.7 38.7 38.7 38.6 38.7 ...
  ..$ tags :'data.frame':	987 obs. of  3 variables:
  .. ..$ id: num [1:987] 2.54e+09 2.54e+09 2.54e+09 2.54e+09 1.34e+09 ...
  .. ..$ k : Factor w/ 100 levels "addr:city","addr:country",..: 21 33 21 33 21 33 21 33 91 21 ...
  .. ..$ v : Factor w/ 344 levels "+7 496 4322013",..: 334 245 334 245 334 245 334 245 339 334 ...
  ..- attr(*, "class")= chr [1:3] "nodes" "osmar_element" "list"
 $ ways     :List of 3
  ..$ attrs:'data.frame':	6082 obs. of  7 variables:
  .. ..$ id       : num [1:6082] 20272219 23323556 23323558 23323560 37783843 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:6082], format: "2015-07-23 11:41:57" "2016-06-22 14:07:50" ...
  .. ..$ version  : num [1:6082] 10 18 6 6 22 7 12 15 7 8 ...
  .. ..$ changeset: num [1:6082] 32823720 40206486 22104907 20303245 46424640 ...
  .. ..$ user     : Factor w/ 62 levels "Павел Гетманцев",..: 28 35 40 50 59 23 33 23 33 40 ...
  .. ..$ uid      : Factor w/ 62 levels "109327","1101663",..: 44 60 39 46 58 36 33 36 33 39 ...
  ..$ tags :'data.frame':	7917 obs. of  3 variables:
  .. ..$ id: num [1:7917] 20272219 20272219 20272219 20272219 20272219 ...
  .. ..$ k : Factor w/ 97 levels "addr:city","addr:country",..: 28 29 34 35 63 67 73 74 91 93 ...
  .. ..$ v : Factor w/ 476 levels "-1","\"Ветерок\"",..: 365 476 5 25 476 41 428 6 400 66 ...
  ..$ refs :'data.frame':	40884 obs. of  2 variables:
  .. ..$ id : num [1:40884] 20272219 20272219 20272219 23323556 23323556 ...
  .. ..$ ref: num [1:40884] 1.30e+09 1.34e+09 1.34e+09 2.54e+08 1.34e+09 ...
  ..- attr(*, "class")= chr [1:3] "ways" "osmar_element" "list"
 $ relations:List of 3
  ..$ attrs:'data.frame':	64 obs. of  7 variables:
  .. ..$ id       : num [1:64] 179639 1693608 3850329 1638020 1638017 ...
  .. ..$ visible  : Factor w/ 1 level "true": 1 1 1 1 1 1 1 1 1 1 ...
  .. ..$ timestamp: POSIXlt[1:64], format: "2017-01-17 08:34:38" "2016-11-17 20:20:16" ...
  .. ..$ version  : num [1:64] 18 5 26 3 1 1 1 1 1 4 ...
  .. ..$ changeset: num [1:64] 45236534 43742492 46963072 18332876 8541357 ...
  .. ..$ user     : Factor w/ 17 levels "Alexander-II",..: 8 14 3 7 9 9 9 12 12 12 ...
  .. ..$ uid      : Factor w/ 17 levels "1101663","1232315",..: 15 7 3 17 6 6 6 1 1 1 ...
  ..$ tags :'data.frame':	308 obs. of  3 variables:
  .. ..$ id: num [1:308] 179639 179639 179639 179639 179639 ...
  .. ..$ k : Factor w/ 105 levels "addr:country",..: 1 3 5 6 9 14 20 90 103 104 ...
  .. ..$ v : Factor w/ 145 levels "007Е;008Е","036Э",..: 128 58 16 66 77 13 37 66 80 120 ...
  ..$ refs :'data.frame':	7996 obs. of  4 variables:
  .. ..$ id  : num [1:7996] 179639 179639 179639 179639 179639 ...
  .. ..$ type: Factor w/ 3 levels "node","relation",..: 3 3 3 3 3 3 3 3 3 3 ...
  .. ..$ ref : num [1:7996] 1.24e+08 1.24e+08 1.24e+08 1.24e+08 1.24e+08 ...
  .. ..$ role: Factor w/ 16 levels "","admin_centre",..: 9 9 9 9 9 9 9 9 9 9 ...
  ..- attr(*, "class")= chr [1:3] "relations" "osmar_element" "list"
 - attr(*, "class")= chr [1:2] "osmar" "list"
In [30]:
users <- sort(unique(bpavposad$nodes$attrs$user))
print(head(users, 12))
 [1] 347929               Владимир К           Наталья Филаретова  
 [4] Павел Гетманцев      Челентано            a_isaeva            
 [7] abel801              ad47                 adekvatno           
[10] alex_konstantino     Alexander Leschinsky AMDmi3              
67 Levels: 347929 Владимир К Наталья Филаретова Павел Гетманцев ... Zloydevol
In [10]:
n <- replicate(20, rnorm(20))
In [11]:
n_data = data.frame(n)
In [12]:
A <- matrix(
    c(0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
      1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
      1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,
      0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0,
      0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
      0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
      0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
      0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0), 
    nrow = 15,               
    ncol = 15,               
    byrow = TRUE)

📑   Maps

In [12]:
pp_gmap <- get_map(location=c(lon = 38.6533, lat = 55.7758), 
                      source = "google", maptype = "hybrid", zoom = 15)
ggmap(pp_gmap, extent = "normal")
Source : https://maps.googleapis.com/maps/api/staticmap?center=55.7758,38.6533&zoom=15&size=640x640&scale=2&maptype=hybrid&language=en-EN
In [15]:
var_ways <- route('GUM Moscow', 'Bolshoy Theater', alternatives = TRUE)
datatable(t(head(data.frame(var_ways))))
Source : https://maps.googleapis.com/maps/api/directions/json?origin=GUM%20Moscow&destination=Bolshoy%20Theater&mode=driving&units=metric&alternatives=true
In [16]:
options(repr.plot.width=6, repr.plot.height=4)
map1 <- ggplot(data = var_ways) + coord_map() +
  geom_leg(aes(x = startLon, xend = endLon, y = startLat, yend = endLat, color = route))
map1
In [17]:
options(repr.plot.width=14, repr.plot.height=5)
qmap(location=c(37.6150, 55.7610), zoom = 13, maptype = 'roadmap', 
     base_layer = ggplot(aes(x = startLon, y = startLat), data = var_ways)) +
  geom_leg(aes(x = startLon, xend = endLon, 
               y = startLat, yend = endLat, color = route), 
           alpha = 0.5, size = 2, data = var_ways) +
  labs(x = 'Longitude', y = 'Latitude', colour = 'Route') +
  facet_wrap(~ route, ncol = 3) + theme(legend.position = 'top')
Source : https://maps.googleapis.com/maps/api/staticmap?center=55.761,37.615&zoom=13&size=640x640&scale=2&maptype=roadmap&language=en-EN
In [36]:
options(repr.plot.width=10, repr.plot.height=10)
plot(bpavposad)
In [32]:
ts <- find(bpavposad, node(tags(v == "traffic_signals")))
ts_pp <- subset(bpavposad, node_ids = ts)
bs <- find(bpavposad, node(tags(v %agrep% "busstop")))
bs_pp <- subset(bpavposad, node_ids = bs)
hw <- find(bpavposad, way(tags(k == "highway")))
hw <- find_down(bpavposad, way(hw))
hw_pp <- subset(bpavposad, ids = hw)
tu <- find(bpavposad, way(tags(k == "tunnel")))
tu <- find_down(bpavposad, way(tu))
tu_pp <- subset(bpavposad, ids = tu)
In [37]:
options(repr.plot.width=14, repr.plot.height=14)
plot_ways(hw_pp, col = "steelblue")
plot_ways(tu_pp, add = TRUE, col = "magenta")
plot_nodes(ts_pp, add = TRUE, col = "red")
plot_nodes(bs_pp, add = TRUE, col = "blue")
In [38]:
bg <- find(bpavposad, way(tags(k == "building")))
bg <- find_down(bpavposad, way(bg))
bg_pp <- subset(bpavposad, ids = bg)
bg_poly <- as_sp(bg_pp, "polygons")
In [41]:
spplot(bg_poly, col.regions=brewer.pal(12, "Set3"), c("version"))
In [42]:
bs_points <- as_sp(bs_pp, "points")
hw_line <- as_sp(hw_pp, "lines")
In [43]:
plot(bg_poly, col = "lightsteelblue")
plot(hw_line, add = TRUE, col = "blue")
plot(bs_points, add = TRUE, col = "red")

📑   Plotting

In [18]:
pl1 <- plot_ly(n_data, x=n_data[1:20,1], y=n_data[1:20,2], z=n_data[1:20,3], color = n_data[1:20,4], 
               text = paste("random variable: ", n_data[1:20,4]), 
               colors = 'Spectral', type="scatter3d", mode="markers", 
               marker = list(size = 10, symbol = "diamond-open"))
pl2 <- layout(pl1, title = "Random Matrix",
         scene = list(xaxis = list(title = "var1"), yaxis = list(title = "var2"), zaxis = list(title = "var3")))
In [19]:
pl2
In [20]:
x <- rnorm(200)
y <- rchisq(200, df = 1, ncp = 0)
group <- sample(LETTERS[1:5], size = 200, replace = T)
size <- sample(1:5, size = 200, replace = T)

ds <- data.frame(x, y, group, size)

pl3 <- plot_ly(ds, type = "scatter", x = x, y = y, mode = "markers", 
               color=group, size = size, opacity=0.7, colors = 'PuRd')
pl4 <- layout(pl3, title = "Scatter Plot")
In [21]:
pl4
In [ ]:

📑   Graphs

In [22]:
g <- graph_from_adjacency_matrix(A, mode = "undirected")
In [26]:
options(repr.plot.width=14, repr.plot.height=7)
plot(g, layout=layout_with_fr,  
     vertex.label.dist=1.5, vertex.label.color='black',
     vertex.size=10, vertex.shape='csquare', vertex.color='steelblue', 
     edge.color='darkblue', edge.arrow.size=2.5)
In [24]:
shortest_paths(g, 4, 12)
$vpath
$vpath[[1]]
+ 6/15 vertices:
[1]  4  2  1  3  6 12


$epath
NULL

$predecessors
NULL

$inbound_edges
NULL
In [28]:
options(repr.plot.width=14, repr.plot.height=7)
g1 <- sample_gnp(60, 1/50)
comps <- components(g1)$membership
colbar <- rainbow(max(comps)+1)
V(g1)$color <- colbar[comps+1]
plot(g1, layout=layout_with_fr, vertex.size=4,
    vertex.label.dist=0.7, vertex.label.color='black')
In [46]:
nodes <- data.frame(id = 1:6, title = paste("node", 1:6), 
                    shape = c("dot", "square"),
                    size = 10:15, color = c("blue", "red"))
edges <- data.frame(from = 1:5, to = c(5, 4, 6, 3, 3))
visNetwork(nodes, edges) %>%
  visOptions(highlightNearest = TRUE, nodesIdSelection = TRUE)
In [ ]:

In [ ]: